home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / pcx2pxo.tproj / pcxheader.c < prev   
Encoding:
C/C++ Source or Header  |  1995-09-23  |  1.9 KB  |  90 lines

  1. #include <stdio.h>
  2. #include <libc.h>
  3.  
  4. #include "pcx.h"
  5.  
  6. int get_short(FILE *fp)
  7. {
  8.     int c = getc(fp);
  9.     return ((getc(fp) << 8) | c);
  10. }
  11.  
  12. pcxHeader *loadPcxHeader(FILE *fp, int *errcode)
  13.      /* ¥à¥¡¥⁄¥º⁄«⁄إ㥈¥¹¬ö˚ú⁄ù˘²⁄ì¡£
  14.     ¥¤¥Ø¡…⁄‹¦fl⁄›⁄¿¬ò„ñ⁄ˇ NULL⁄‹˚á⁄Œ¡¢errcode⁄¸⁄‰⁄˛˝ÿ˝‡⁄‹˘⁄º¡£ */
  15. {
  16.     int i, x1, y1, err;
  17.     pcxHeader *ph;
  18.     unsigned char *pp;
  19.     paltype *pal;
  20.  
  21.     *errcode = err = 0;
  22.     if (getc(fp) != pcxMAGIC) {
  23.         *errcode = Err_FORMAT;
  24.         return NULL;
  25.     }
  26.     if ((ph = (pcxHeader *)malloc(sizeof(pcxHeader))) == NULL) {
  27.         *errcode = Err_MEMORY;
  28.         return NULL;
  29.     }
  30.     if ((pal = (paltype *)malloc(sizeof(paltype) * 16)) == NULL) {
  31.         free((void *)ph);
  32.         *errcode = Err_MEMORY;
  33.         return NULL;
  34.     }
  35.     ph->version = getc(fp);
  36.     ph->comp = getc(fp);
  37.     ph->bits = getc(fp);
  38.     x1 = get_short(fp);    /* (x,y) min. */
  39.     y1 = get_short(fp);
  40.     ph->x = get_short(fp) - x1 + 1;  /* get width and height */
  41.     ph->y = get_short(fp) - y1 + 1;
  42.     ph->xpm = get_short(fp);    /* resolution */
  43.     ph->ypm = get_short(fp);
  44.     for (i = 0; i < 16; i++) { /* get palette */
  45.         pp = pal[i];
  46.         pp[RED]   = getc(fp);
  47.         pp[GREEN] = getc(fp);
  48.         pp[BLUE]  = getc(fp);
  49.     }
  50.     ph->palette = pal;
  51.     (void) getc(fp);    /* skip 1 byte */
  52.     ph->planes = getc(fp);
  53.     ph->xbytes = get_short(fp);
  54.     ph->pinfo = get_short(fp);
  55.  
  56.     if (ph->comp > 1)
  57.         err = Err_IMPLEMENT;
  58.     else {
  59.         if (ph->bits == 1) {
  60.             if (ph->planes > 4)
  61.                 err = Err_ILLG;
  62.             if (ph->planes != 1 && ph->planes != 4)
  63.                 err = Err_IMPLEMENT;
  64.         }else if ((ph->bits != 2 && ph->bits != 4 && ph->bits != 8)
  65.             || ph->planes != 1)
  66.             err = Err_ILLG;
  67.         if (ph->x <= 0 || ph->y <= 0)
  68.             err = Err_ILLG;
  69.     }
  70.     if (err) {
  71.         *errcode = err;
  72.         freePcxHeader(ph);
  73.         return NULL;
  74.     }
  75.     sprintf(ph->memo,
  76.         "%dbit%s/%dplane%s,  ver=%d,  comp=%d",
  77.         ph->bits, ((ph->bits > 1) ? "s" : ""),
  78.         ph->planes, ((ph->planes > 1) ? "s" : ""),
  79.         ph->version,  ph->comp);
  80.     return ph;
  81. }
  82.  
  83. void freePcxHeader(pcxHeader *ph)
  84. {
  85.     if (ph) {
  86.         if (ph->palette) free((void *)ph->palette);
  87.         free((void *)ph);
  88.     }
  89. }
  90.